<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/11/22
 * Time: 16:59
 */

namespace app\console\controller;
use think\Db;
use think\Request;
use think\Session;
use think\Loader;
use think\Url;
class Salesman
{
    /**
     * @return array|mixed|\think\response\View
     * 业务员管理
     */
    public function my_team()
    {

        $data = Db::name('salesman s')
            ->join('manager m','s.manage_id = m.id','left')
            ->field('m.username,m.nickname,m.create_time,s.*,m.id,m.telephone')
            ->where('m.level',4)
            ->select()->toArray();
        if(!empty($data)){
            foreach ($data as $k => &$v){
                $v['member_count'] = Db::name('share_member')->where('manage_id',$v['manage_id'])->count();;
                $v['son_count'] = 1;

            }
        }

        $parent_id = 0;
        $auding_status = Db::name('manager m')->where('m.id',session('manage_id'))->join('auth_group ag','m.level = ag.id')->value('auding_status');
        if($auding_status == 1){
            $parent_id = session('manage_id');
        }
        $data = getMenuTree1($data,$parent_id);

        return view('',['data' => $data]);
    }

    public function update($id){
        if(Request::instance()->isPost()){

            $ret = Db::name('salesman')->fetchSql(false)->where('manage_id',$id)->update(['parent_id' => Request::instance()->param('parent_id')]);

            if($ret !== false){
                win('修改成功',Url::build('my_team'));
            }
            fail('修改失败');
        }
        $data = Db::name('salesman s')
            ->join('manager m','s.manage_id = m.id','left')
            ->field('m.username,m.nickname,m.create_time,s.*,m.id,m.telephone')
            ->where('m.level',4)
            ->select()->toArray();
        $info = Db::name('salesman s')
            ->join('manager m','s.manage_id = m.id','left')
            ->field('m.username,m.nickname,m.create_time,s.*,m.id,m.telephone')
            ->where('m.level',4)
            ->where('manage_id',$id)
            ->find();

        $data = getMenuTree1($data,0);

        return view('',['data' => $data,'item' => $info]);
    }

    /**
     * @return array|mixed|\think\response\View
     * 我的信息
     */
    public function my_information()
    {
        $data = Db::name('manager')->where('id', Session::get('manage_id'))->find();
        $order_number = $data['id'];
        Loader::import('phpqrcode.phpqrcode', EXTEND_PATH);
        $object = new \QRcode();
        $path = ROOT_PATH . 'public/salesman/' . $order_number . '.png';
        //第二个参数false的意思是不生成图片文件，如果你写上‘picture.png’则会在根目录下生成一个png格式的图片文件
        $url = Request::instance()->domain().'/index.php/mobile/user_port/share_member/manage_id/'.$data['id'];
        $ret = $object->png($url, $path, 10, 10, 2);
        $son = Db::name('salesman s')
            ->join('manager m','s.manage_id = m.id','left')
            ->field('m.username,m.create_time,s.*,m.id,m.telephone')
            ->select()->toArray();
        $parent_id = 0;
        $auding_status = Db::name('manager m')->where('m.id',session('manage_id'))->join('auth_group ag','m.level = ag.id')->value('auding_status');
        if($auding_status == 1){
            $parent_id = session('manage_id');
        }
        $son_data = getMenuTree1($son,$parent_id);
        $share_number = Db::name('share_member')->where('manage_id',Session::get('manage_id'))->count();
        return view('', ['data' => $data,'son_data' => $son_data,'share_number' => $share_number]);
    }

    public function member($id){
        $member_share = Db::name('share_member sm')
            ->join('member m','sm.member_id = m.id')
            ->field('m.*,sm.manage_id')
            ->where('manage_id',$id)
            ->select();

        $share_count = Db::name('share_member')->where('manage_id',$id)->count();
        return view('', ['member_share' => $member_share]);
    }

    /**
     * @param $member_id
     * @return array
     * 我的我二维码
     */
    public function update_level($manage_id)
    {
        if(Request::instance()->isPost()){
            $son = Db::name('salesman')->where('parent_id',$manage_id)->select();//当前管理唯一子集
            $new_parent_id = Request::instance()->param('parent_id');
            foreach ($son as $k => $v){
                Db::name('salesman')->where('manage_id',$v['manage_id'])->update(['parent_id' => $new_parent_id]);
            }

            win('修改成功',Url::build('my_team'));
        }


        $son = Db::name('salesman s')
            ->join('manager m','s.manage_id = m.id','left')
            ->field('m.username,m.nickname,m.create_time,s.*,m.id,m.telephone')
            ->where('m.level',4)
            ->select()->toArray();
        $son_str = implode(',',sonPath($son,$manage_id,true));

        $son_data = getMenuTree1($son,0);
        $info = Db::name('salesman s')
            ->where('s.manage_id',$manage_id)
            ->find();

    return view('',['son_date' => $son_data,'son_str' => $son_str,'info' => $info]);
    }
}